﻿@using GameSchool.HtmlHelpers
@using GameSchool.Models.AccountModels
@model GameSchool.ViewModels.Course.EditStudentsViewModel
@{
    ViewBag.Title = "Nemendur í " + Model.currentCourse.Code;
}
<h3>
    Nemendur í @Model.currentCourse.Code, @Model.currentCourse.Name,
    @Model.currentCourse.Semester.Year-@Model.currentCourse.Semester.SemesterOfYear</h3>
<table id="course-students" class="table table-striped">
    <tr>
        <th>
            Nafn
        </th>
        <th>
            Leiðbeinandi
        </th>
        <th>
            Nemandi
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model.courseStudents.OrderBy(i => AccountProfile.GetProfileByUserId(i.StudentUserID).FullName))
    {
        @Html.Partial("AddStudentPartial", item)
    }
</table>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
@using (Html.BeginForm(htmlAttributes: new { @class = "well form-search", id = "add-student" }))
{
    <div>
        <fieldset>
            <legend>Bæta við nemanda eða leiðbeinanda</legend>
            <input type="hidden" value="@Model.currentCourse" />
            <input type="text" name="studentFullname" id="add-student-name" class="input-xlarge search-query"
                   data-provide="typeahead" placeholder="Leita í nemendalista"
                   data-val="true" data-val-required="Það þarf að fyllla rétt út í nafn úr nemendalistanum"
                   data-source='@{ Html.RenderAction("GetStudents"); }'>
            <label class="radio">
                <input type="radio" checked="checked" name="isInstructor" value="false">
                Nemandi
            </label>
            <label class="radio">
                <input type="radio" name="isInstructor" value="true">
                Leiðbeinandi
            </label>
            <button type="submit" class="btn" id="add-student-submit">
                Bæta við</button>
            <br />
            <span class="field-validation-valid" data-valmsg-for="studentFullname" data-valmsg-replace="true">
            </span>
        </fieldset>
    </div>
}
<div id="currentCourseId" hidden="hidden">@Model.currentCourse.CourseID</div>
<script type="text/javascript">
    // Virknin fyrir takkann til að fjarlægja nemanda úr áfanga
    $('.course-student-remove').on('click', function (event) {
        // koma í veg fyrir að linkurinn virki venjulega
        event.preventDefault();

        var trToRemove = $(this).parents('tr.course-student:first');
        var courseId = $(this).siblings('div.course-student-courseid:first').text();
        var studentUserId = $(this).siblings('div.course-student-studentUserId:first').text();
        var callingUrl = '/Course/RemoveStudent/' + courseId + '?studentUserId=' + studentUserId;

        $.get(callingUrl, function () {
            trToRemove.remove();
        });
    });

    $('#add-student').on('submit', function (event) {
        // koma í veg fyrir venjulegt form submit
        event.preventDefault();
        
        // validatea formið
        var $form = $(this);
        if ($form.valid()) {
            // finna studentName til að geta validateað það
            var studentName = $('#add-student-name').val();

            if (studentName.indexOf('(') > -1 && studentName.indexOf(')') > -1) {
                // studentName stóðst basic validation, tími til að pósta forminu
                var url = '/Course/AddStudent/' + $('#currentCourseId').text();

                $.post(url, $('#add-student').serialize(), function (html) {
                    $('#course-students').append(html);
                    $('#add-student-name').val('');
                });
            }
        }
    });

    $(document).ready(function () {
        $('.typeahead').typeahead();
        $(".alert").alert();
    });
</script>
